package com.wannengbang.cloudleader.mine;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.google.gson.Gson;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.bean.UserInfoBean;
import com.wannengbang.cloudleader.utils.DateTimeUtil;
import com.wannengbang.cloudleader.utils.SPManager;
import com.wannengbang.cloudleader.utils.VersionUtil;
import com.wannengbang.cloudleader.widget.AppTitleBar;

import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AboutUsActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.tv_about)
    TextView tvAbout;
    @BindView(R.id.tv_protocol1)
    TextView tvProtocol1;
    @BindView(R.id.tv_protocol2)
    TextView tvProtocol2;
    @BindView(R.id.tv_protocol3)
    TextView tvProtocol3;
    private String start_time;
    private String end_time;
    private int timeType = 1;
    private Calendar selectedDate;
    private TimePickerView pvCustomTime;
    private TextView tvStartTime;
    private TextView tvEndTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_us);
        ButterKnife.bind(this);
        tvVersion.setText("当前版本  V" + VersionUtil.packageName(this));

        tvProtocol1.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tvProtocol1.getPaint().setAntiAlias(true);//抗锯齿
        tvProtocol2.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tvProtocol2.getPaint().setAntiAlias(true);//抗锯齿
        tvProtocol3.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tvProtocol3.getPaint().setAntiAlias(true);//抗锯齿

        start_time = DateTimeUtil.format(new Date());
        end_time = DateTimeUtil.format(new Date());
        selectedDate = Calendar.getInstance();

        UserInfoBean userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
        if (userInfoBean != null) {
            //7标记隐藏
            if ("17329409714".equals(userInfoBean.getData().getMobile()) || "13403854628".equals(userInfoBean.getData().getMobile())) {
                tvAbout.setText("\t\t\t云创共享是一个合伙人的数据查询平台，可以查询自己每天的订单、交易、收益、业绩和合伙人数量。");
            } else {
                tvAbout.setText("\t\t\t付临门支付有限公司成立于2009年，隶属于银嘉金服集团，总部坐落于上海徐汇区天华信息科技园，2011年获得中国人民银行颁发的支付业务许可证，是国内线下支付领军企业。付临门以科技为核心，不断完善产品线，服务中小微商户和个人消费者，为服务实体经济，促进普惠金融做出积极贡献。\n\t\t\t云创共享是付临门的战略合作平台，在传统支付行业日趋衰落的形式下，云创共享平台融合支付传统模式和联盟模式的优势之处，努力营造一个良性的、生态的新支付环境，解决现有支付环境的痛点与不足，让支付人真正拥有一个自己的创业平台。");
            }
        }
    }

    @OnClick({R.id.tv_protocol1, R.id.tv_protocol2, R.id.tv_protocol3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_protocol1:
                Intent intent = new Intent(mActivity, UserAgreementActivity.class);
                intent.putExtra("type", 1);
                startActivity(intent);
                break;
            case R.id.tv_protocol2:
                intent = new Intent(mActivity, UserAgreementActivity.class);
                intent.putExtra("type", 2);
                startActivity(intent);
                break;
            case R.id.tv_protocol3:
                intent = new Intent(mActivity, UserAgreementActivity.class);
                intent.putExtra("type", 4);
                startActivity(intent);
                break;
        }
    }

    public void selectTime() {
        timeType = 1;
        selectedDate.setTime(DateTimeUtil.parse(start_time));
        //这里可以更改参数，有好多类型
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        startDate.set(1900, 0, 1);
        endDate.set(2099, 11, 31);
        pvCustomTime = new TimePickerBuilder(mActivity, (date, v) -> {
            if (timeType == 1) {
                start_time = DateTimeUtil.format(date);
                if (tvStartTime != null) {
                    tvStartTime.setText(start_time);
                }
            } else {
                end_time = DateTimeUtil.format(date);
                if (tvEndTime != null) {
                    tvEndTime.setText(end_time);
                }
            }
        }).setType(new boolean[]{true, true, true, false, false, false})
                .setTimeSelectChangeListener(date -> {
                    if (timeType == 1) {
                        if (tvStartTime != null) {
                            tvStartTime.setText(DateTimeUtil.format(date));
                        }
                    } else {
                        if (tvEndTime != null) {
                            tvEndTime.setText(DateTimeUtil.format(date));
                        }
                    }
                })
                .setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        TextView tvSubmit = v.findViewById(R.id.tv_finish);
                        TextView ivCancel = v.findViewById(R.id.tv_cancel);
                        tvStartTime = v.findViewById(R.id.tv_start_time);
                        tvEndTime = v.findViewById(R.id.tv_end_time);
                        View viewLine1 = v.findViewById(R.id.view_line1);
                        View viewLine2 = v.findViewById(R.id.view_line2);
                        LinearLayout llStartTime = v.findViewById(R.id.ll_start_time);
                        LinearLayout llEndTime = v.findViewById(R.id.ll_end_time);

                        tvStartTime.setText(start_time);
                        tvEndTime.setText(end_time);

                        llStartTime.setOnClickListener(v1 -> {
                            timeType = 1;
                            tvStartTime.setTextColor(mActivity.getResources().getColor(R.color.theme_color));
                            viewLine1.setBackgroundColor(mActivity.getResources().getColor(R.color.theme_color));
                            tvEndTime.setTextColor(mActivity.getResources().getColor(R.color.text_color_2));
                            viewLine2.setBackgroundColor(mActivity.getResources().getColor(R.color.text_color_2));

                            selectedDate.setTime(DateTimeUtil.parse(tvStartTime.getText().toString()));
                            pvCustomTime.setDate(selectedDate);


                        });
                        llEndTime.setOnClickListener(v1 -> {
                            timeType = 2;
                            tvStartTime.setTextColor(mActivity.getResources().getColor(R.color.text_color_2));
                            viewLine1.setBackgroundColor(mActivity.getResources().getColor(R.color.text_color_2));
                            tvEndTime.setTextColor(mActivity.getResources().getColor(R.color.theme_color));
                            viewLine2.setBackgroundColor(mActivity.getResources().getColor(R.color.theme_color));

                            selectedDate.setTime(DateTimeUtil.parse(tvEndTime.getText().toString()));
                            pvCustomTime.setDate(selectedDate);


                        });

                        tvSubmit.setOnClickListener(v1 -> {
                            pvCustomTime.returnData();
                            pvCustomTime.dismiss();
                        });
                        ivCancel.setOnClickListener(v1 -> {
                            pvCustomTime.dismiss();
                        });
                    }
                })
                .setSubmitColor(getResources().getColor(R.color.text_color_2))//确定按钮文字颜色
                .setSubCalSize(14)
                .setCancelColor(getResources().getColor(R.color.text_color_2))//取消按钮文字颜色
                .setTitleBgColor(getResources().getColor(R.color.white))
                .setDividerColor(getResources().getColor(R.color.line_color_gray))
                .setTextColorCenter(getResources().getColor(R.color.text_color_2))
                .setContentTextSize(15)
                .setLineSpacingMultiplier(2f)
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)//起始终止年月日设定
                .isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .build();

        Dialog mDialog = pvCustomTime.getDialog();
        if (mDialog != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvCustomTime.getDialogContainerLayout().setLayoutParams(params);

            Window window = mDialog.getWindow();
            if (window != null) {
                window.setWindowAnimations(R.style.picker_view_slide_anim);//修改动画样式
                window.getDecorView().setPadding(0, 0, 0, 0);
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                lp.gravity = Gravity.BOTTOM;
            }
        }
        pvCustomTime.show();
    }
}
